package bjtu.annotation.helper;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@EnableAutoConfiguration 
public abstract class BaseController {
	protected Logger logger;
	protected static ObjectMapper mapper  = new ObjectMapper();
	public static final ResCode SUCCESS = new ResCode(0,"success");
	public static final ResCode ERROR = new ResCode(400,"error");
	public static final ResCode ParamsError = new ResCode(400,"params error");

	protected void logInfo(Object message) {
		this.logger = Logger.getLogger(this.getClass());
		logger.info(message);
	}
	protected String jsonDump(Object object) {
		try {
			return mapper.writeValueAsString(object);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return jsonDump(ERROR);
		}
	}
	protected <T> T fromJson(String content,Class<T> clz){
		try {
			return mapper.readValue(content, clz);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}
